package com.openx.xoj.judge.strategy;

import cn.hutool.json.JSONUtil;
import com.openx.xoj.model.dto.question.JudgeCase;
import com.openx.xoj.model.dto.question.JudgeConfig;
import com.openx.xoj.judge.codesandbok.mode.JudgeInfo;
import com.openx.xoj.model.entity.Question;
import com.openx.xoj.model.entity.QuestionSubmit;
import com.openx.xoj.model.enums.JudgeInfoMessageEnum;

import java.util.List;
import java.util.Optional;
import java.util.Random;

/**
 * 默认判题策略
 */
public class DefaultJudgeStrategy implements JudgeStrategy {
    /**
     * 执行判题
     * @param judgeContext
     * @return
     */
    @Override
    public JudgeInfo doJudge(JudgeContext judgeContext) {
        JudgeInfo judgeInfo = judgeContext.getJudgeInfo();
        //TODO: 内存占用
        Random random = new Random();
        long gaussian = (long)(random.nextGaussian() * 3 + 0.8);//生成高斯分布随机数(均值为5,标准差为2)
        Long memory = Optional.ofNullable(judgeInfo.getMemory()).orElse(gaussian);
        Long time = Optional.ofNullable(judgeInfo.getTime()).orElse(0L);
        List<String> inputList = judgeContext.getInputList();
        List<String> outputList = judgeContext.getOutputList();
        List<JudgeCase> judgeCaseList = judgeContext.getJudgeCaseList();
        Question question = judgeContext.getQuestion();
        QuestionSubmit questionSubmit = judgeContext.getQuestionSubmit();
        JudgeInfoMessageEnum judgeInfoMessageEnum = JudgeInfoMessageEnum.ACCEPTED;
        JudgeInfo judgeInfoResponse = new JudgeInfo();
        judgeInfoResponse.setMemory(memory);
        judgeInfoResponse.setTime(time);
        //先判断沙箱执行结果输出的数量是否和预期输出的数量一致
        if(outputList.size() != inputList.size()){
            judgeInfoMessageEnum = JudgeInfoMessageEnum.WRONG_ANSWER;
            judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
        }
        // 依次判断每一项输出和预期输出是否相等(输入一个样例,必将输出一个样例,所以两者的数量必须是一致的)
        for (int i = 0; i < outputList.size(); i++) {
            JudgeCase judgeCase = judgeCaseList.get(i);
            if(!outputList.get(i).equals(judgeCase.getOutput())){
                judgeInfoMessageEnum = JudgeInfoMessageEnum.WRONG_ANSWER;
                judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
                return judgeInfoResponse;
            }
        }
        //判题题目限制
        String judgeConfigStr = question.getJudgeConfig();
        JudgeConfig judgeConfig = JSONUtil.toBean(judgeConfigStr, JudgeConfig.class);//将json字符串转换为JudgeConfig对象
        Long memoryLimit = judgeConfig.getMemoryLimit();
        Long timeLimit = judgeConfig.getTimeLimit();

        if(memory > memoryLimit){//判断内存和时间是否超出限制
            judgeInfoMessageEnum = JudgeInfoMessageEnum.MEMORY_LIMIT_EXCEEDED;
            judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
            return judgeInfoResponse;
        }

        if(time > timeLimit){//判断内存和时间是否超出限制
            judgeInfoMessageEnum = JudgeInfoMessageEnum.TIME_LIMIT_EXCEEDED;
            judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
            return judgeInfoResponse;
        }
        judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
        return judgeInfoResponse;
    }
}
